import cv2
import cv2.cv2
import numpy as np
import matplotlib.pyplot as plt

# 私人相机是640*480分辨率

# # 读取图像
# img = cv2.imread('F:/Code/opencv/opencv_python/data/text.png')
# print(img.shape)
# # cv2.imshow('img', img)
# # cv2.waitKey(0)
# # cv2.destroyAllWindows()
# image=cv2.imread('F:/Code/opencv/opencv_python/data/text.png',cv2.IMREAD_GRAYSCALE)
# cv2.imshow('img', image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# # 保存
# cv2.imwrite('1.png',image)
# print(image.shape)

# print(type(img))
# print(img.size)


vc=cv2.VideoCapture(0)
if vc.isOpened():
    open,frame=vc.read()
else:
    open=False
# top_size,bottom_size,left_size,right_size=(50,50,50,50)

# text=cv2.imread('1.png')
# print(text.shape)
# cv2.imshow('text',text)
# text_resize=cv2.resize(text,(640,480))
# print(text_resize.shape)
# cv2.imshow('text_resize',text_resize)


# 卷积核
kernel=np.ones((5,5),np.uint8)

while open:
    ret,frame=vc.read()
    if ret==True:
        # gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        # 全部显示二值化图像显示
        gray_image=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        binary_image=cv2.threshold(gray_image,150,255,cv2.THRESH_BINARY)[1]
        

        image_e=cv2.erode(binary_image,kernel,iterations=3)
        image_d=cv2.dilate(image_e,kernel,iterations=3)
        res=np.hstack((image_d,image_e,binary_image))
        # cv2.imshow('res',res)
        image_g1=cv2.morphologyEx(binary_image,cv2.MORPH_GRADIENT,kernel)
        dst=cv2.Sobel(binary_image,cv2.CV_64F,1,0,ksize=3)
        dst=cv2.convertScaleAbs(dst)
        cv2.imshow('dst',dst)
        # cv2.imshow('binary_image',binary_image)
        # cv2.imshow('image_g',image_g)
        gray_roi=binary_image[0:480,0:640]
        gray_roi=cv2.erode(gray_roi,kernel,iterations=1)
        # cv2.imshow('gray_roi',gray_roi)
        # gray_roi=cv2.copyMakeBorder(gray_roi,top_size,bottom_size,left_size,right_size,cv2.BORDER_REPLICATE)
        # # gray_roi+=0
        # gray_roi=gray_roi[0:480,0:640]
        # print(gray_roi.shape)
        # gray_resize=cv2.resize(gray_roi,(0,0),fx=0.8,fy=0.8)
        # image_rong=cv2.addWeighted(frame,0.6,text_resize,0.2,0.2)
        # cv2.imshow('frame',image_rong)
        # print(gray_roi.shape)
      
        # print(gray_resize.shape)
  
        if cv2.waitKey(1)==ord('q'):
            break
    else:
        break
vc.release()

# while open :
#     ret,frame=vc.read()
#     if ret==True:
#         cv2.imshow('frame',frame)
#         b,g,r=cv2.split(frame)
#         print(b,g,r)
#         if cv2.waitKey(1)==ord('q'):
#             break
#     else:
#         break
# vc.release()
# cv2.destroyAllWindows()



